home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / human interface toolbox / dialog popups / dialogtest.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  6.0 KB  |  209 lines

  1. /*
  2.     File:        DialogTest.c
  3.  
  4.     Contains:    snippet to demonstrate the use of the system 7 popup 
  5.                 control cdef in a program using modal dialogs
  6.  
  7.                 Sponge ID:105648       New Question
  8.   
  9.                 The content of your link dated: 31 May 1994 follows:
  10.                 ******************************************************************************
  11.                 ------------------------------------------------------------------------------
  12.  
  13.                 I’m not sure who I should refer this to, but I believe I have found a bug in
  14.                 the new Dialog Manager routines SetDialogDefaultItem & GetStdFilterProc.
  15.  
  16.                 What I am trying to do is create a default button that is initially grayed out
  17.                 and later enabled when the dialog box is filled in.  What is happening is that
  18.                 my button is getting re-enabled (unless I step into the source debugger).  My
  19.                 other non default button is getting grayed out as expected.  I tried
  20.                 rearranging the order that things were done to get it to stay gray, but I
  21.                 couldn’t.  I finally had to resort to using a filter proc and a global variable
  22.                 to tell me if I should gray the button after the initial call to
  23.                 GetStdFilterProc.
  24.   
  25.                 ******************************************************************************
  26.  
  27.     Written by: Nick Thompson    
  28.  
  29.     Copyright:    Copyright © 1994-1999 by Apple Computer, Inc., All Rights Reserved.
  30.  
  31.                 You may incorporate this Apple sample source code into your program(s) without
  32.                 restriction. This Apple sample source code has been provided "AS IS" and the
  33.                 responsibility for its operation is yours. You are not permitted to redistribute
  34.                 this Apple sample source code as "Apple sample source code" after having made
  35.                 changes. If you're going to re-distribute the source, we require that you make
  36.                 it clear in the source that the code was descended from Apple sample source
  37.                 code, but that you've made changes.
  38.  
  39.     Change History (most recent first):
  40.                 8/5/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  41.                 
  42.  
  43. */
  44. #include <Menus.h>
  45. #include <Processes.h>
  46. #include <Dialogs.h>
  47. #include <Fonts.h>
  48.  
  49. pascal Boolean OurFilter(DialogPtr dlg, EventRecord *event, short *itemHit) ;
  50.  
  51. const char kEnter    = 0x03 ;
  52. const char kReturn    = 0x0D ;
  53. const char kEscape    = 0x1B ;
  54. const char kPeriod    = '.' ;
  55.  
  56. const char kThePopupMenu = 4 ;        // popup is the fourth item in the dialog
  57. const char kUserTextArea = 5 ;        // this is a rect for us to write a string
  58.                                     // with the item chosen in it
  59.  
  60.  
  61. // Structure for the private data for a popup control.
  62. // This structure is documented on page 5-77 
  63. // Inside Macintosh: Macintosh Toolbox Essentials
  64.  
  65. typedef struct popupPrivateData {
  66.     MenuHandle     mHandle;     // the popup menu handle 
  67.     short         mID;        // the popup menu ID 
  68.     // after these two public fields is the mPrivate private data, 
  69.     // which may be any old size and should not be messed with 
  70. }    popupPrivateData;
  71.  
  72.  
  73.  
  74.  
  75. void main(void) {
  76.  
  77.     DialogPtr             thePopupDialog ;
  78.     short                itemHit ;
  79.     popupPrivateData    **myPopupPrivateDataPtr ;
  80.     
  81.     short                iKind;
  82.     Handle                iHandle;
  83.     Rect                iRect;
  84.     
  85.     MenuHandle            thePopupMenuHdl ;
  86.     Str255                theItem ;
  87.     
  88.     OSErr                 theErr ;
  89.     
  90.     // initialize the toolbox
  91.     InitGraf(&qd.thePort); InitFonts(); InitWindows(); InitMenus();
  92.     TEInit(); InitDialogs((long)nil); InitCursor(); FlushEvents(everyEvent,0);
  93.     
  94.     thePopupDialog = GetNewDialog ( 128, nil, (WindowPtr)-1 );
  95.  
  96.     SetPort( (GrafPtr)thePopupDialog ) ;
  97.     theErr =  SetDialogDefaultItem(thePopupDialog, ok) ;
  98.  
  99.     if( theErr != noErr )
  100.         ExitToShell() ;
  101.  
  102.     
  103.     do {
  104.     
  105.         ModalDialog ( NewModalFilterProc(OurFilter), &itemHit );
  106.         
  107.         if( itemHit == kThePopupMenu ) {
  108.         
  109.             // the user choose the popup.  The item number selected will be the control value
  110.             // we need to get the menuhandle associated with the control, it is in the private
  111.             // control data field, as documented in Inside Macintosh: Toolbox page 5-77
  112.             
  113.             // get the control handle for the popup            
  114.             GetDialogItem ( thePopupDialog, kThePopupMenu, &iKind, &iHandle, &iRect) ;
  115.             
  116.             // extract from the control the menuhandle
  117.             myPopupPrivateDataPtr = (popupPrivateData **)(**(ControlHandle)iHandle).contrlData ; 
  118.             thePopupMenuHdl = (**myPopupPrivateDataPtr).mHandle ;
  119.     
  120.             // get the string associated with the users selection
  121.             GetMenuItemText ( thePopupMenuHdl, GetControlValue((ControlHandle)iHandle), theItem );
  122.             
  123.             // get the rect we are drawing in
  124.             GetDialogItem ( thePopupDialog, kUserTextArea, &iKind, &iHandle, &iRect) ;
  125.             SetDialogItemText ( iHandle, theItem );
  126.             
  127.             // this ensures that the update handler in the filter proc is called
  128.             // as that is where we enable or disable the OK button
  129.             InvalRect( &iRect ) ;
  130.             
  131.             // reset itemHit to something else or we'll continually redraw
  132.             itemHit = 0 ;
  133.         
  134.         }
  135.         
  136.     } while( itemHit != ok ) ;
  137.     
  138.     DisposeDialog ( thePopupDialog );
  139.  
  140. }
  141.  
  142.  
  143.  
  144.  
  145.  
  146. pascal Boolean OurFilter(DialogPtr dlg, EventRecord *event, short *itemHit)
  147. {
  148.  
  149.     ModalFilterUPP         theProc ;
  150.     Boolean                retVal ;
  151.     
  152.     static    Boolean        isDisabled = false ;
  153.     OSErr                theErr = noErr ;
  154.     
  155.     // stuff for getditems etc
  156.     Str255                theItem ;
  157.     short                iKind;
  158.     Handle                iHandle;
  159.     Rect                iRect;
  160.     
  161.     
  162.     // get the std filter proc
  163.     
  164.     theErr = GetStdFilterProc( &theProc ) ;
  165.     
  166.     if( theErr != noErr )
  167.         ExitToShell() ;
  168.         
  169.     // try to call the standard filter, if it handles the event, we don't
  170.     if( !(retVal = CallModalFilterProc(theProc, dlg, event, itemHit)) )
  171.     {
  172.         switch (event->what) {
  173.     
  174.             case nullEvent:
  175.                 break;
  176.     
  177.             case keyDown:
  178.             case autoKey:
  179.                 retVal = false;
  180.                 break ;
  181.     
  182.             case updateEvt:
  183.                 // get the text item we are drawing in
  184.                 GetDialogItem ( dlg, kUserTextArea, &iKind, &iHandle, &iRect) ;
  185.                 GetDialogItemText ( iHandle, theItem );
  186.                 isDisabled = theItem[0] == '\0' ;
  187.                 
  188.                 // enable or disable the OK button depending on whether 
  189.                 // we made a selection yet
  190.                 GetDialogItem(  dlg, ok, &iKind, &iHandle, &iRect) ;
  191.                 if( isDisabled )
  192.                     HiliteControl( (ControlHandle)iHandle, 255 ) ;
  193.                 else
  194.                     HiliteControl( (ControlHandle)iHandle, 0 ) ;
  195.                     
  196.                 retVal = false;
  197.                 break ;
  198.     
  199.             default:
  200.                 retVal = false;
  201.                 break ;
  202.         }
  203.     }
  204.     
  205.     return retVal ;
  206. }
  207.  
  208.  
  209.